﻿#ifndef LDCOMMINC_H
#define LDCOMMINC_H

//================================================================================
//v2.00:






//=================================================================================



#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <elog.h>

//#include <delaychan.h>
#include <errno.h>


#ifdef WIN32
#include <WinSock2.h>
#include <ws2ipdef.h>
#include <pthread.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/videodev2.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <sys/file.h>
#define  SOCKET   int
#define  WSAGetLastError    errorno
#define  closesocket        close
#endif


#include <pthread.h>
#include <sched.h>

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/core/persistence.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/tracking.hpp>


#include "im2d.h"
#include "RgaUtils.h"
#include "RgaApi.h"



#define MAX_ALG_FPS         (50)

#define FRAME_MAX_LEN        (1920*1200*3)

#define  NET_GROUP_ADD      ("226.0.0.2")

#define SERVO_PACKET_BYTE  (240)

enum enResVal
{
    LD_START_UPGRADE_V2_SUCCESS = 4,
    LD_START_UPGRADE_THREAD = 3, //启动升级线程
    LD_STEP_UPGRADE = 2,  //进入软件升级流程
    LD_RES_FORWARD  = 1,  //消息需要转发返回值
    LD_RES_OK = 0,       //消息处理成功返回值
    LD_RES_FAILURE   =  -1,
    LD_RES_MSG_ERROR =  -2,
    LD_RES_UDP_SEND_FAIL = -3,
    LD_RES_TMP_HALF_FRAME = -4,
    LD_RES_RGA_ERROR = -5,
    LD_RES_PARA_ERROR = -6,
    LD_RES_CAMERA_ERR = -7,
};

typedef enum
{
    CHN_VISIBLE,
    CHN_IR,
    CHN_SIM
}EN_VIDEOCHN;

extern "C"  void ldMemCpy(unsigned char* dst, const unsigned char* src, int size);
extern "C"  void ldMemSet(unsigned char* dst,  unsigned char ch, int size);




#define STR_BUFF_LEN          (512)
#define MAX_PATH_LEN          (1200)
#define LD_DEV_NAME_LEN       (256)
#define OBJ_RECT_MARGIN       (0)
#define MAX_SERIAL_PACK_LEN   (2048)


#define MAX_FRAME_BUFF_CNT     (6)   //视频帧缓冲区个数


#define UDP_SEND_INTER        (5) //视频或者图片上传5ms间隔

#define VIDEO_800K_LIMIT      (480000)


#ifndef SAFE_DELETE
#define SAFE_DELETE(p) {if(p) {delete (p);(p)=NULL;}}
#endif

#ifndef SATE_STOP_THREAD
#define SATE_STOP_THREAD(pThread) {   \
    if(pThread)   \
    {  pThread->Stop();    \
       pThread->wait(); \
       if(pThread) {delete (pThread);(pThread)=NULL;}    \
    }}
#endif





#ifndef SAFE_DELETE_ARRAY
#define SAFE_DELETE_ARRAY(p) {if(p) {delete[] (p);(p)=NULL;}}
#endif

#define  MAX_UDP_MSG          (8*1024)


#define FRAME_PIC_LEN        (1920*1080*3)

#define INFRARED_MAX_LEN     (720*576*3)
#define VIS_PIC_HEIGHT        (1080)
#define VIS_PIC_WIDTH        (1920)
#define VIEW_ANGLE           (18.8)
typedef enum
{
    LD_RTSP_MIX,    //
    LD_RTSP_BUTT,
}LD_RTSP_CHN;


#define  SERIAL_BUFFLEN   (512)

typedef enum
{
   COMM_COM_RELAY,
   COMM_BUTT
}EN_CONN_PORT;

#define ALG_MAX_FPS            (30)
#define ALG_OBJ_SIZE_MIN       (16) //注：目标大小>16x16像素
#define ALG_OBJ_PAD            (5)
#define ALG_MAX_OBJ_SIZE       (800)

#define GUI_FONT_SIZE          (36)
#define MAX_VIDEO_LOST         (5)

#define UDP_MSG_MAX_LEN        (1500)  //udp消息最大长度，仅仅适用于以太网，无线或者要修改
#define UDP_BROAD_ADD          "255.255.255.255"
#define UDP_BROAD_PORT         (9001)


#define  INVALID_CERTER_POS   (0xFFFFFFFF)




#define GET_BIT(x,bit) ((x & (1 << bit)) >> bit)
#define	CLEAR_BIT(x, bit)	(x &= ~(1 << bit))	/* 清零第bit位 */
#define	SET_BIT(x, bit)	(x |= (1 << bit))	/* 置位第bit位 */

#define CURSOR_PIX_LEN    (25)


#define DETECT_OBJ_COLOR  (cv::Scalar(0,0,255))  //检测对象使用蓝框

#define TRACK_OBJ_COLOR  (cv::Scalar(255,0,0))   //跟踪对象使用红色框

#define  IS_VIS_CHN(VALX)      (!GET_BIT(VALX,0))  //是可见光通道
#define  IS_INFRA_CHN(VALX)    (GET_BIT(VALX,0))  //是红外通道

#define XLOGD(format, ...)                                                      \
{                                                                               \
    log_i("[%s:%d]" format, __FILE__, __LINE__, ##__VA_ARGS__);   \
}


#define MAX_MSG_LEN   64
//惠视板卡解码的红外视频 宽度和高度，被用在了跟踪坐标计算的的地方，这个很特殊，或者需要最后调整

#define  INFRARED_WIDTH      (640)  //惠视板卡解码的红外视频宽度 720X576
#define  INFRARED_HEIGHT     (512)  //惠视板卡解码的红外视频高度


#define   OSD_PIC_WIDTH      (320)
#define   OSD_PIC_HEIGHT     (240)

//要关闭nanotrack fix pro

//打开#define   HUI_SHI_RK3588_DEV_BRD    则支持惠视，关闭支持迅为的开发板
//#define   HUI_SHI_RK3588_DEV_BRD    //惠视rk3588 开发板

//#define   EN_NANOTRACK        1    //

#define   DEF_TRACK_ALG      TRACK_KCF

#ifdef WIN32
int gettimeofday (struct timeval *tv, void* tz);
#endif

#define GET_BIT(x,bit) ((x & (1 << bit)) >> bit)
#define	CLEAR_BIT(x, bit)	(x &= ~(1 << bit))	/* 清零第bit位 */
#define	SET_BIT(x, bit)	(x |= (1 << bit))	/* 置位第bit位 */



#define   MAX_YOLO_DET_OBJ    (10)


typedef struct
{
    int                  nChannel;
    int                  nWidth;
    int                  nHeight;
    uint8_t              ucData[FRAME_MAX_LEN];

    int                  nDataLen;
    int                  nSeq;
    uint64_t             nPts;
    struct timeval       nTimeStamp;
    float                fMinScale;
}stVFrame;


#endif // LDCOMMINC_H
